/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package makao.server;

import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author latata_2
 */
public class Timer extends Thread {

    private int time = 0;
    private boolean zeruj = false;
    private SendPackageController sendPackageController;

    public Timer(SendPackageController sendPackageController) {
        this.sendPackageController = sendPackageController;
    }

    @Override
    public void run() {
        while (true) {

            try {
                Thread.sleep(1000);
            } catch (InterruptedException ex) {
                Logger.getLogger(Timer.class.getName()).log(Level.SEVERE, null, ex);
            }
            time++;
            if (zeruj) {
                zeruj = false;
                time = 0;
            }
            if (time == 180) {
                if (sendPackageController.getServer().getServerState() == Server.STATE_GAME_STARTED) {
                    Player player = sendPackageController.getServer().getGame().getWhoseTurn();
                    sendPackageController.getServer().getGame().killUser();     
                    player.getPlayerConnection().disconnect();
                    sendPackageController.getServer().getPlayers().remove(player);
                    sendPackageController.sendGameInfo();
                }
            }
        }
    }

    public void zeruj() {
        this.zeruj = true;
    }
}
